package com.luyuan.crm.member.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.model.MemberIntegral;
import com.luyuan.crm.member.model.MemberIntegralReceived;
import com.luyuan.crm.member.model.MemberIntegralRecord;
import com.luyuan.crm.member.service.MemberIntegralReceivedService;
import com.luyuan.crm.member.service.MemberIntegralRecordService;
import com.luyuan.crm.member.service.MemberIntegralService;
import com.ruijie.foundation.dao.support.HibernateEntityDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

public class MemberIntegralReceivedServiceImpl extends HibernateEntityDao<MemberIntegralReceived> implements
        MemberIntegralReceivedService {
    private MemberIntegralService memberIntegralService;

    private MemberIntegralRecordService memberIntegralRecordService;

    @Override
    public void addMemberIntegralReceived(MemberIntegralReceived memberIntegralReceived) {
        memberIntegralReceived.setReceivedTime(new Date());
        save(memberIntegralReceived);

        // 积分记录
        MemberIntegral memberIntegral = memberIntegralService.getMemberIntegral(memberIntegralReceived.getMember());
        MemberIntegralRecord usedRecord = new MemberIntegralRecord();
        usedRecord.setMember(memberIntegralReceived.getMember());
        usedRecord.setUsedTime(new Date());
        usedRecord.setIntegral(0);
        usedRecord.setTotalIntegral(memberIntegral.getTotalIntegral());
        usedRecord.setUsedDetail("赠送积分，请于三个月内领取:\r\n" + memberIntegralReceived.getDescription());
        memberIntegralRecordService.addMemberIntegralRecord(usedRecord);
    }

    @Override
    public void clearOverDateIntegral() {
        logger.info("clean over time MemberIntegralReceived ");
        updateBySQL("memberIntegralReceived.clearOverDateIntegral.record", new Date());
        update("delete MemberIntegralReceived where overTime < ?", new Date());
    }

    @Override
    public List<MemberIntegralReceived> receiveMemberIntegral(Member member, String remark) {
        List<MemberIntegralReceived> memberIntegralReceiveds = findBy("member", member);
        if (memberIntegralReceiveds == null || memberIntegralReceiveds.isEmpty()) {
            return memberIntegralReceiveds;
        }

        MemberIntegral memberIntegral = memberIntegralService.getMemberIntegral(member);
        for (MemberIntegralReceived memberIntegralReceived : memberIntegralReceiveds) {
            int integral = memberIntegralReceived.getIntegral();
            String r = memberIntegralReceived.getDescription();
            memberIntegralService.updateMemberCurrentIntegral(memberIntegral, integral,
                    r.replaceFirst("赠送积分，请于三个月内领取", remark));
        }
        // 清除已经领用积分
        update("DELETE MemberIntegralReceived m where m.member = ?", member);
        return memberIntegralReceiveds;
    }

    @Override
    public Page pagedQuery(CriteriaTemplate c, int page, int pageSize, List<Order> orders) {
        CriteriaQueryTemplate qt = new CriteriaQueryTemplate(getSessionFactory());
        return qt.pagedQuery(c, page, pageSize, orders);
    }

    @Override
    public int sumReceiveMemberIntegral(Member member) {
        List<?> s = find("select sum(m.integral) from MemberIntegralReceived m where m.member = ? group by m.member",
                member);
        if (s == null || s.isEmpty()) {
            return 0;
        }
        return ((Long) s.get(0)).intValue();
    }

    public void setMemberIntegralRecordService(MemberIntegralRecordService memberIntegralRecordService) {
        this.memberIntegralRecordService = memberIntegralRecordService;
    }

    public void setMemberIntegralService(MemberIntegralService memberIntegralService) {
        this.memberIntegralService = memberIntegralService;
    }
}
